package com.example.day14.homework;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

// TODO: 2022/8/4
public class TestAnnoUser {
    public static void main(String[] args) {
        Map<Object,Object> map=new HashMap<>();
        //获取class实例
        Class<?> c=User.class;

        String id = null;
        String ntype=null;
        String table_name = null;

        //判断改类上是否有Entity注解
        if(c.isAnnotationPresent(Entity.class)){
            //判断类上是否有Table注解
            if(c.isAnnotationPresent(Table.class)){
               Table  table=c.getDeclaredAnnotation(Table.class);
                 table_name = table.value();
                Field[] declaredFields = c.getDeclaredFields();
                boolean flag=false;
                for (Field declaredField : declaredFields) {
                    if (declaredField.isAnnotationPresent(ID.class)){
                        flag=true;
                        id= declaredField.getName();
                        ntype=declaredField.getType().getSimpleName();
                        map.put(id,ntype);
                    }else {
                        map.put(declaredField.getName(),declaredField.getType().getSimpleName());
                    }

                }
                if(!flag){
                    System.out.println("没有找到id的注解。。。");
                }

            }
        }
        map.put("name","varcahr");


        System.out.println("drop table "+table_name+" ;");
        System.out.println("create table "+table_name +" (");
        System.out.println( "  Id  "+  map.get(id)+"  primary key ,");
        System.out.println( "  name  "+  map.get("name")+" ,");
        System.out.println( "  age  "+  map.get("age")+" ,");
        System.out.println( "  price  "+  map.get("price")+" )");

    }
}
